require('dotenv').config();
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const app = express();

// 中间件
app.use(cors());
app.use(helmet());
app.use(morgan('dev'));
app.use(express.json());

// 路由
const authRoutes = require('./routes/auth.route');
const dessertRoutes = require('./routes/desserts');
const categoryRoutes = require('./routes/categories.route');
const sectionRoutes = require('./routes/sections.route');
const userRoutes = require('./routes/users.route');


app.use('/api/auth', authRoutes);
app.use('/api/desserts', dessertRoutes);
app.use('/api/categories', categoryRoutes);
app.use('/api/sections', sectionRoutes);
app.use('/api/users', userRoutes);

// 错误处理
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({ message: 'Internal Server Error' });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});